package com.aacar.controller.wash;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.aacar.dto.Header;
import com.aacar.dto.wash.UserDto;
import com.aacar.service.wash.WashLoginService;
import com.aacar.utils.ErrorCodeConsField;
import com.aacar.utils.ErrorInfoUtil;
import com.aacar.utils.Pphconfig;
import com.aacar.utils.ResultInfoUtil;
import com.aacar.vo.UserVo;
import com.aacar.vo.wash.WasherVo;
import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.StringUtils;


/**
 * 登录
 * @author wangh
 */
@RestController
@RequestMapping("/wash/login")
public class WasherLoginController {
	
	private Logger logger = LoggerFactory.getLogger("washlogin");
	
	@Autowired
	private WashLoginService washLoginService;
	
	
	@RequestMapping(value = "/washlogin/0", method = RequestMethod.POST)
	public Object login(@RequestHeader(value="mobilehead") String mobilehead,@RequestBody UserDto userDto) {
		logger.info("request info = /wash/login/washlogin/0");
		Header header = null;
    	if(null != mobilehead) {
    		header = JSON.parseObject(mobilehead, Header.class);
    	}
    	String serviceCode = header.getService();
		if(StringUtils.isNullOrEmpty(userDto.getPhoneNo())){
    		return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10200, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10200),serviceCode);
    	}
    	if(StringUtils.isNullOrEmpty(userDto.getPassWord())){
    		return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10201, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10201),serviceCode);
    	}
    	WasherVo washVo = this.washLoginService.getWashUserInfo(userDto);
    	if (washVo == null) {
    		//账户不存在
    		return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10202, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10202),serviceCode);
        }else if(! washVo.getLoginPwd().equals(userDto.getPassWord())) {
        	return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10203, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_10203),serviceCode);
        }
		return ResultInfoUtil.setSuccessInfo(washVo,serviceCode);
	}
	
	
}
